package com.autonavi.yunda.yunji.domain;

import com.autonavi.yunda.yunji.common.enums.EditType;
import com.autonavi.yunda.yunji.common.exception.AmapException;
import com.autonavi.yunda.yunji.common.exception.AmapExceptionCode;
import com.autonavi.yunda.yunji.core.enums.ParamType;
import com.autonavi.yunda.yunji.core.vo.node.BaseNode;
import com.autonavi.yunda.yunji.core.vo.node.GroovyFunctionNode;
import com.autonavi.yunda.yunji.core.vo.node.SqlNode;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * @author cizhong.wcs
 * @date 2023/9/22 17:24
 */
@Data
@NoArgsConstructor
public class DebugSingleNodeVO {
    private BaseNode baseNode;
    private Long interfaceId;
    private Long functionId;
    /**
     * 编排类型 task:任务 interface:接口
     */
    private String editType;

    public  DebugSingleNodeVO(JsonNode jsonNode) {
        // JsonNode对象 转为 BaseNode对象
        BaseNode baseNode = BaseNode.parseNode(jsonNode);
        // Sql节点和function节点需要解码
        baseNode = decodeNode(baseNode);
        JsonNode interfaceId = jsonNode.get("interfaceId");
        JsonNode functionId = jsonNode.get("functionId");
        JsonNode editType = jsonNode.get("editType");
        this.baseNode = baseNode;
        this.interfaceId = interfaceId == null ? null : interfaceId.asLong();
        this.functionId = functionId == null ? null : functionId.asLong();
        this.editType = editType == null ? EditType.INTERFACE.name() : editType.asText();
    }

    public DebugSingleNodeVO(MultipartHttpServletRequest request) {
        try {
            JsonNode jsonNode = new ObjectMapper().readTree(request.getParameterMap().get("jsonNode")[0]);
            JsonNode interfaceId = jsonNode.get("interfaceId");
            JsonNode functionId = jsonNode.get("functionId");
            JsonNode editType = jsonNode.get("editType");
            BaseNode baseNode = BaseNode.parseNode(jsonNode);
            baseNode = decodeNode(baseNode);
            if (!baseNode.params.isEmpty()) {
                baseNode.params.forEach(param -> {
                    if (ParamType.FILE.getCode().equals(param.type)) {
                        param.setDebugValue(request.getFile(param.name));
                    }
                });
            }
            this.baseNode = baseNode;
            this.interfaceId = interfaceId == null ? null : interfaceId.asLong();
            this.functionId = functionId == null ? null : functionId.asLong();
            this.editType = editType == null ? EditType.INTERFACE.name() : editType.asText();
        } catch (JsonProcessingException e) {
            throw new AmapException(AmapExceptionCode.COMMON_ILLEGAL_PARAM, "参数解析异常", e);
        }
    }

    /**
     * decode 节点中的一些字段如 sql
     */
    public static BaseNode decodeNode(BaseNode baseNode) {
        try {
            if (baseNode instanceof SqlNode && ((SqlNode) baseNode).sql != null) {
                String sql = ((SqlNode) baseNode).sql;
                ((SqlNode) baseNode).sql = new String(Base64.getDecoder().decode(sql.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
            }
            if (baseNode instanceof GroovyFunctionNode && ((GroovyFunctionNode) baseNode).script != null) {
                String script = ((GroovyFunctionNode) baseNode).script;
                ((GroovyFunctionNode) baseNode).script = new String(Base64.getDecoder().decode(script.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
            }
        } catch (IllegalArgumentException e) {

        }
        return baseNode;
    }
}
